package main

import (
	"fmt"
)

func main() {
	messages := make(chan string)
	signals := make(chan bool)

	go func() {
		messages <- "one"
	}()

	select {
	case msg := <-messages:
		fmt.Println("received message", msg)
	default:
		fmt.Println("no message received")
	}

	var msg = "hi"
	//注意：这里会打印default语句, 原因是channel的初始容量是0，并且没有创建协程给channel添加数据
	// 解决办法： 第一种是给创建的channel设置初始容量，比如 messages := make(chan string,1),给初始的容量设置成1，那么信道中就可以发送数据
	//          第二种是创建协程，go func(){ messages <- msg  }()
	select {
	case messages <- msg:
		fmt.Println("sent message", msg)
	default:
		fmt.Println("no message sent")
	}

	select {
	case msg := <-messages:
		fmt.Println("received message", msg)
	case sig := <-signals:
		fmt.Println("received signal", sig)
	default:
		fmt.Println("no activity")
	}

}
